<?php

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Support\Facades\DB;

class PlatFinanceModel extends BaseModel
{
    protected $table = 'plat_finance';

    protected $primaryKey = 'id';

    public $dateFormat = 'U';

    protected $guarded = [];

    public function countRgMoney(float $money)
    {
        $data['total_rg'] = DB::raw('IFNULL(`total_rg`,0)+'.$money);
        $data['count_rg'] = DB::raw('IFNULL(`count_rg`,0)+1');
        $map['date'] = $data['date'] = Carbon::today()->timestamp;
        return self::updateOrInsert($map,$data);
    }

    public function incomeFinance(float $money)
    {
        $data['income'] = DB::raw('IFNULL(`income`,0)+'.$money);
        $data['fact_income'] = DB::raw('IFNULL(`fact_income`,0)+'.$money);
        $map['date'] = $data['date'] = Carbon::today()->timestamp;
        return self::updateOrInsert($map,$data);
    }

    public function expendFinance(float $money)
    {
        $data['expend'] = DB::raw('IFNULL(`expend`,0)+'.$money);
        $data['fact_expend'] = DB::raw('IFNULL(`fact_expend`,0)+'.$money);
        $map['date'] = $data['date'] = Carbon::today()->timestamp;
        return self::updateOrInsert($map,$data);
    }

}
